#!/bin/sh
#
# Test branch checkout (GitHub issue #290)

. libtest.sh
. libgit.sh

export LINES=9
export COLUMNS=100

steps '
	:save-display refs-init.screen
	:3
	:exec @sh -c '\''printf "refs=%s\\nbranch=%s\\ntag=%s\\n" "%(ref)" "%(branch)" "%(tag)" > $HOME/%(commit).refinfo'\''
	:exec @git checkout -q %(branch)
	:2
	:save-display refs-checked-out-branch.screen
	:exec @sh -c "git rev-parse HEAD > $HOME/%(ref).head"
	:6
	:exec @sh -c '\''printf "refs=%s\\nbranch=%s\\ntag=%s\\n" "%(ref)" "%(branch)" "%(tag)" > $HOME/%(commit).refinfo'\''
	:exec @git checkout -q %(tag)
	:save-display refs-checked-out-tag.screen
	:7
	:exec @sh -c "git rev-parse HEAD > $HOME/%(ref).head"
'

LONG_BRANCH_NAME_ID=19455fa3642af6a6a7d527dd043caf5a70eaad2d
LONG_BRANCH_NAME=this_is_really_a_long_long_long_long_long_long_long_branch

LONG_TAG_NAME_ID=a3f25ca556c1cef1ef3a0704f5deaec8687c84b9
LONG_TAG_NAME=this_is_really_a_long_long_long_long_long_long_long_tag

create_branches()
{
	git branch "$LONG_BRANCH_NAME" HEAD~4
	git tag "$LONG_TAG_NAME" HEAD~9
}

git_clone 'repo-one'
in_work_dir create_branches

test_tig refs

assert_equals "$LONG_BRANCH_NAME_ID.refinfo" <<EOF
refs=$LONG_BRANCH_NAME
branch=$LONG_BRANCH_NAME
tag=
EOF

assert_equals "$LONG_BRANCH_NAME.head" <<EOF
$LONG_BRANCH_NAME_ID
EOF

assert_equals "$LONG_TAG_NAME_ID.refinfo" <<EOF
refs=$LONG_TAG_NAME
branch=
tag=$LONG_TAG_NAME
EOF

assert_equals "$LONG_TAG_NAME.head" <<EOF
$LONG_TAG_NAME_ID
EOF

assert_equals 'refs-init.screen' <<EOF
                                     All references
2010-04-07 05:37 +0000 Max Power     master                                                     Comm
2010-03-04 04:09 +0000 A. U. Thor    this_is_really_a_long_long_long_long_long_long_long_branch Comm
2010-04-07 05:37 +0000 Max Power     origin/master                                              Comm
2010-04-07 05:37 +0000 Max Power     origin/HEAD                                                Comm
2010-01-20 14:18 +0000 A. U. Thor    this_is_really_a_long_long_long_long_long_long_long_tag    Comm
2009-12-17 12:49 +0000 René Lévesque v1.0                                                       Comm
[refs] All references                                                                           100%
EOF

assert_equals 'refs-checked-out-branch.screen' <<EOF
                                     All references
2010-03-04 04:09 +0000 A. U. Thor    this_is_really_a_long_long_long_long_long_long_long_branch Comm
2010-04-07 05:37 +0000 Max Power     master                                                     Comm
2010-04-07 05:37 +0000 Max Power     origin/master                                              Comm
2010-04-07 05:37 +0000 Max Power     origin/HEAD                                                Comm
2010-01-20 14:18 +0000 A. U. Thor    this_is_really_a_long_long_long_long_long_long_long_tag    Comm
2009-12-17 12:49 +0000 René Lévesque v1.0                                                       Comm
[refs] 19455fa3642af6a6a7d527dd043caf5a70eaad2d - reference 1 of 6                              100%
EOF

assert_equals 'refs-checked-out-tag.screen' <<EOF
                                     All references
2010-01-20 14:18 +0000 A. U. Thor    HEAD                                                       Comm
2010-04-07 05:37 +0000 Max Power     master                                                     Comm
2010-03-04 04:09 +0000 A. U. Thor    this_is_really_a_long_long_long_long_long_long_long_branch Comm
2010-04-07 05:37 +0000 Max Power     origin/master                                              Comm
2010-04-07 05:37 +0000 Max Power     origin/HEAD                                                Comm
2010-01-20 14:18 +0000 A. U. Thor    this_is_really_a_long_long_long_long_long_long_long_tag    Comm
[refs] 5cb3412a5e06e506840495b91acc885037a48b72 - reference 5 of 7                               87%
EOF
